#include "../decoder_conf.h"

#if DECODER_FS == DECODER_FS_POSIX
#include "../decoder_fs.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void *DecoderFs::openFile(const char *path)
{
    long fd = open(path, O_RDONLY);
    if (fd < 0)
        return nullptr;

    return (void *)fd;
}

void DecoderFs::closeFile(void *file)
{
    close((long)file);
}

void DecoderFs::seekFile(void *file, int pos, Whence whence)
{
    lseek((long)file, pos, whence);
}

size_t DecoderFs::readFile(void *file, uint8_t *buf, size_t cnt)
{
    uint32_t br = read((long)file, buf, cnt);
    return (int32_t)br < 0 ? 0 : br;
}

size_t DecoderFs::sizeofFile(void *file)
{
    struct stat stat;

    fstat((long)file, &stat);
    
    return stat.st_size;
}

#endif